import math
N = 0
a = 10
y = 0
def IsPrime (y):
    if y == 1 :
        return 1
    else:
        q = 0
        s = math.sqrt(y)
        for i in range (2,(round(s))+2):    
            if y % i == 0 :
                q += 1
        if q == 0 :
            return 1
        else:
            return 0
b=int(input())
if b < 1000 or b > 9999:
    print(0)
else: 
    y = b   
    if IsPrime(y) == 1 :
        print(b)
    else:
        for i in range (3):
            y = b % a
            if IsPrime(y) == 1 and y > N:
                N = y
            y = b // a
            if IsPrime(y) == 1 and y > N:
                N = y
            a*=10
        y = (b // 10)%100
        if IsPrime(y) == 1 and y > N:
            N = y
        print(N)
